
//
//  FaceViewController.m
//  新浪的弹出动画
//
//  Created by MyMac on 15/8/21.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "FaceViewController.h"

@interface FaceViewController ()

@end

@implementation FaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
//    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
//    view.backgroundColor    =[UIColor yellowColor];
//    [self.view addSubview:view];
//    UIScrollView*backView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
//    [self.view  addSubview:backView];
//    backView.contentSize=CGSizeMake(1000, 1000);
//    self.view.backgroundColor=[UIColor whiteColor];
//    //人脸识别
    UIImage*image=[UIImage imageNamed:@"屏幕快照 2015-08-21 下午7.06.20.png"];
    
  
    
  //  UIImageView*AimageView=[[UIImageView alloc]initWithImage:image];
    
   UIImage*newImage=[self imageFormImage:image];
    UIImageView*imageView=[[UIImageView alloc]initWithImage:newImage];
    [self.view addSubview:imageView];
   // [AimageView setTransform:CGAffineTransformMakeScale(1, -1)];
    //AimageView.transform=CGAffineTransformMakeRotation(M_PI);
    
  // [[[UIApplication sharedApplication] delegate].window setTransform:CGAffineTransformMakeScale(1, -1)];
  //  [AimageView setFrame:CGRectMake(0, 0, AimageView.image.size.width,
    //                               AimageView.image.size.height)];
    //AimageView.transform=CGAffineTransformMakeRotation(M_PI);
    
//    AimageView.image=image;
//    [backView addSubview:AimageView];
    
    CIImage*Cimage=[CIImage imageWithCGImage:newImage.CGImage];
    NSLog(@"%ld",newImage.imageOrientation);
    //CIContext*text=[CIContext contextWithOptions:nil];
    CIDetector*detector=[CIDetector detectorOfType:CIDetectorTypeFace context:nil options:[NSDictionary dictionaryWithObjectsAndKeys:CIDetectorAccuracyHigh,CIDetectorAccuracy,nil]];
    NSArray*array=[detector featuresInImage:Cimage];
    for(NSInteger i=0;i<array.count;i++){
        CIFaceFeature*feature=array[i];
        CGFloat w=self.view.bounds.size.width/3.0;
        CGFloat h=w;
        CGFloat x=i%3*w;
        CGFloat y=i/3*h;
        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
        [self.view addSubview:imageView];
        imageView.backgroundColor=[UIColor redColor];
        NSLog(@"%@",NSStringFromCGRect(feature.bounds));
        
    
        CGImageRef ref=CGImageCreateWithImageInRect(newImage.CGImage, feature.bounds);
        imageView.image=[UIImage imageWithCGImage:ref];
        CGImageRelease(ref);
        
        
//        UIView*view=[[UIView alloc]initWithFrame:feature.bounds];
//        view.backgroundColor=[UIColor redColor];
//        [backView addSubview:view];
    
    }
    
    //二维码识别，不知为何识别不出来
//    UIImage*image=[UIImage imageNamed:@"屏幕快照 2015-08-21 下午6.56.57.png"];
//    CIImage*Cimage=[CIImage imageWithCGImage:[UIImage imageNamed:@"屏幕快照 2015-08-21 下午6.56.57.png"].CGImage];
//    CIContext*text=[CIContext contextWithOptions:nil];
//    CIDetector*detector=[CIDetector detectorOfType:CIDetectorTypeQRCode context:text options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
//    NSArray*array=[detector featuresInImage:Cimage];
//    for(NSInteger i=0;i<array.count;i++){
//        CIQRCodeFeature *feature=array[i];
//        CGFloat w=self.view.bounds.size.width/3.0;
//        CGFloat h=w;
//        CGFloat x=i%3*w;
//        CGFloat y=i/3*h;
//        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
//        [self.view addSubview:imageView];
//        NSLog(@"%@",NSStringFromCGRect(feature.bounds));
//        CGImageRef ref=CGImageCreateWithImageInRect(image.CGImage, feature.bounds);
//        imageView.image=[UIImage imageWithCGImage:ref];
//        
//    }

 //   }
   // [self faceSelector];
}
-(void)faceSelector
{
    UIScrollView*scroll=[[UIScrollView alloc]initWithFrame:self.view.bounds];
    scroll.contentSize=CGSizeMake(1000, 1000);
    [self.view addSubview:scroll];
    UIImage* image = [UIImage imageNamed:@"屏幕快照 2015-08-21 下午7.06.20.png"];
    UIImageView* testImage = [[UIImageView alloc]initWithImage:image];
     testImage.image=image;
    [testImage setTransform:CGAffineTransformMakeScale(1, -1)];
    [[[UIApplication sharedApplication] delegate].window setTransform:CGAffineTransformMakeScale(1, -1)];
    [testImage setFrame:CGRectMake(0, 0, testImage.image.size.width,
                                   testImage.image.size.height)];
    [scroll addSubview:testImage];
    
    CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage];
    NSDictionary* opts = [NSDictionary dictionaryWithObject:
                          CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil options:opts];
    NSArray* features = [detector featuresInImage:ciimage];
    
    for (CIFaceFeature *faceFeature in features){
        
        CGFloat faceWidth = faceFeature.bounds.size.width;
        
        // create a UIView using the bounds of the face
        UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
        //2015-08-24 10:58:52.981 新浪的弹出动画[4721:34287] {{178, 90}, {82, 82}}
        //        2015-08-24 10:58:52.982 新浪的弹出动画[4721:34287] {{362, 171}, {38, 38}}
        //        2015-08-24 10:58:52.982 新浪的弹出动画[4721:34287] {{504, 83}, {36, 36}}
        //        2015-08-24 10:58:52.983 新浪的弹出动画[4721:34287] {{583, 75}, {34, 34}}
        //        2015-08-24 10:58:52.983 新浪的弹出动画[4721:34287] {{374, 86}, {30, 30}}
        
        NSLog(@"%@",NSStringFromCGRect(faceFeature.bounds));
        // add a border around the newly created UIView
        
        faceView.layer.borderWidth = 1;
        faceView.layer.borderColor = [[UIColor redColor] CGColor];
        
        [scroll addSubview:faceView];
        
        if(faceFeature.hasLeftEyePosition)
            
        {
            // create a UIView with a size based on the width of the face
            
            UIView* leftEyeView = [[UIView alloc] initWithFrame:
                                   CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15,
                                              faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
            
            // change the background color of the eye view
            [leftEyeView setBackgroundColor:[[UIColor blueColor]
                                             colorWithAlphaComponent:0.3]];
            
            // set the position of the leftEyeView based on the face
            [leftEyeView setCenter:faceFeature.leftEyePosition];
            
            // round the corners
            leftEyeView.layer.cornerRadius = faceWidth*0.15;
            
            // add the view to the window
            [scroll  addSubview:leftEyeView];
            
        }
        
        if(faceFeature.hasRightEyePosition)
            
        {
            // create a UIView with a size based on the width of the face
            UIView* leftEye = [[UIView alloc] initWithFrame:
                               CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15,
                                          faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
            
            // change the background color of the eye view
            [leftEye setBackgroundColor:[[UIColor blueColor]
                                         colorWithAlphaComponent:0.3]];
            
            // set the position of the rightEyeView based on the face
            [leftEye setCenter:faceFeature.rightEyePosition];
            
            // round the corners
            leftEye.layer.cornerRadius = faceWidth*0.15;
            
            // add the new view to the window
            [scroll  addSubview:leftEye];
        }
        
        if(faceFeature.hasMouthPosition)
        {
            
            // create a UIView with a size based on the width of the face
            UIView* mouth = [[UIView alloc] initWithFrame:
                             CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2,
                                        faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)];
            
            // change the background color for the mouth to green
            [mouth setBackgroundColor:[[UIColor greenColor]
                                       colorWithAlphaComponent:0.3]];
            
            // set the position of the mouthView based on the face
            [mouth setCenter:faceFeature.mouthPosition];
            
            // round the corners
            mouth.layer.cornerRadius = faceWidth*0.2;
            
            // add the new view to the window
            [scroll  addSubview:mouth];
        }         
    }

}
-(UIImage*)imageFormImage:(UIImage*)image{
  //  CGSize newSize=rect.size;
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context=UIGraphicsGetCurrentContext();
//    CGContextTranslateCTM(context, newSize.width/2, newSize.height/2);
//    CGContextConcatCTM(context, transform);
//    CGContextTranslateCTM(context, newSize.width/-2, newSize.height/-2);
    CGContextScaleCTM(context, 1, -1);
    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
    UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;







}

@end
